#!/usr/bin/python

from sklearn import svm
import numpy

execfile('/opt/python/includes/finance.py')

selRbf=svm.SVC(kernel='rbf',gamma=0.7,C=1.0,verbose=True, probability=True)
buyRbf=svm.SVC(kernel='rbf',gamma=0.7,C=1.0,verbose=True, probability=True)

stocks=getIndexComponents('^FTSE')
#stocks=['LLOY.L', 'RBS.L', 'STAN.L', 'TSCO.L', 'RRS.L']
#stocks=['LLOY.L']
index=['^FTSE']

inputs={}
X=[]
T=[]
ybuy=[]
tbuy=[]
ysel=[]
tsel=[]
datelist=(getOHLCDict(index[0]))['date']

for stock in stocks:
    prices=getOHLCDict(stock,'ohlcObjects')
    if(len(prices)<260): continue
    print str(len(prices))+" price values for "+stock
    for i in range(len(prices)-1,0,-1):
        if prices[i]['date'] in datelist: continue
        del prices[i]
    for i in range(len(prices)-5):
        prices[i]['close_delta1'] = prices[i]['close']-prices[i+1]['close']
        prices[i]['close_delta2'] = prices[i]['close']-prices[i+2]['close']
        prices[i]['close_delta3'] = prices[i]['close']-prices[i+3]['close']
        prices[i]['close_delta4'] = prices[i]['close']-prices[i+4]['close']
        prices[i]['open_delta1'] = prices[i]['open']-prices[i+1]['open']
        prices[i]['open_delta2'] = prices[i]['open']-prices[i+2]['open']
        prices[i]['open_delta3'] = prices[i]['open']-prices[i+3]['open']
        prices[i]['open_delta4'] = prices[i]['open']-prices[i+4]['open']
        if not prices[i]['date'] in inputs.keys():
            inputs[prices[i]['date']]=[]
        inputs[prices[i]['date']].append(prices[i]['close_delta1'])
        inputs[prices[i]['date']].append(prices[i]['close_delta2'])
        inputs[prices[i]['date']].append(prices[i]['close_delta3'])
        inputs[prices[i]['date']].append(prices[i]['close_delta4'])
        inputs[prices[i]['date']].append(prices[i]['open_delta1'])
        inputs[prices[i]['date']].append(prices[i]['open_delta2'])
        inputs[prices[i]['date']].append(prices[i]['open_delta3'])
        inputs[prices[i]['date']].append(prices[i]['open_delta4'])

prices=getOHLCDict(index[0],'ohlcObjects')
for i in range(len(prices)-1):
    if not prices[i+1]['date'] in inputs.keys(): continue
    print "Calculating output for date "+str(prices[i]['date'])
    print "Using inputs from "+str(prices[i+1]['date'])
    if(i%10!=0):
        if(prices[i]['close']>prices[i]['open']):
            ybuy.append(1)
            ysel.append(0)
        else:
            ybuy.append(0)
            ysel.append(1)
        X.append(numpy.array(inputs[prices[i+1]['date']]))
    else:
        if(prices[i]['close']>prices[i]['open']):
            tbuy.append(1)
            tsel.append(0)
        else:
            tbuy.append(0)
            tsel.append(1)
        T.append(numpy.array(inputs[prices[i+1]['date']]))

print T
selRbf.fit(X,ysel)
buyRbf.fit(X,ybuy)

correct=0
fail=0
for i in range(len(T)):
    sellPrediction=selRbf.predict(T[i])
    buyPrediction=buyRbf.predict(T[i])
    print selRbf.predict_proba(T[i])
    print buyRbf.predict_proba(T[i])

    if(tbuy[i]==1 and sellPrediction==0 and buyPrediction==1): correct=correct+1
    if(tbuy[i]==1 and sellPrediction==1 and buyPrediction==0): fail=fail+1

    if(tsel[i]==1 and sellPrediction==1 and buyPrediction==0): correct=correct+1
    if(tsel[i]==1 and sellPrediction==0 and buyPrediction==1): fail=fail+1

print "After "+str(i)+" predictions, "+str(correct)+" were correct and "+str(fail)+" were incorrect"
  
